package risoe.syslab.fhnew.gui;

/*
 * Copyright (c) 2012-2014, Technical University of Denmark (DTU)
 * All rights reserved.
 * 
 * The Flexhouse 2.0 platform ("this software") is licensed under the
 * BSD 3-clause license.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of DTU nor the names of its contributors may be used to
 *     endorse or promote products derived from this software without specific
 *     prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE TECHNICAL UNIVERSITY OF DENMARK BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.net.URI;
import java.util.ArrayList;

import org.w3c.dom.svg.SVGDocument;

/**
 * Class representing the internal(non-gui) state of a visualization. 
 * @author M. Svendsen
 */
public class VisualizationHolder {
	private SVGDocument mDocument;
	private final String mName;
	private URI mURI;
	private String mSerializedDocument;
	private final ArrayList<String> mSubscribedObjects = new ArrayList<String>(0);
	private ArrayList<DocumentPreparedListener> mDocumentPreparedListeners;
	private ArrayList<VisualizationEventListener> mVisualizationEventListeners;

	/**
	 * Constructs a new VisualizationHolder to be inflated by the UIManager
	 * @param name the name of the associated visualization/controller
	 * @param uri the path to the SVG file which should be inflated represented as an URI
	 */
	public VisualizationHolder(String name, URI uri) {
		mName = name;
		mURI = uri;
	}
	
	/**
	 * Constructs a new VisualizationHolder to be inflated by the UIManager
	 * @param name the name of the associated visualization/controller
	 * @param serializedDocument a string representing a serialized SVG Document
	 */
	public VisualizationHolder(String name, String serializedDocument) {
		mName = name;
		mURI = null;
		mSerializedDocument = serializedDocument;
	}

	/**
	 * Sets the SVG document for use with this visualizationholder.
	 * NOTE: This should ONLY get called from the UIManager
	 * @param document the SVGDocument representing the SVG file of this visualizationholder
	 */
	public void setDocument(SVGDocument document){
		mDocument = document;
	}

	/**
	 * @return a string representing the name of the contained visualization
	 */
	public String getName(){
		return mName;
	}

	/**
	 * @return an uri representing the path of the SVG file for the contained visualization
	 */
	public URI getPath(){
		return mURI;
	}

	/**
	 * @return the SVGDocument associated with this visualizationholder
	 */
	public SVGDocument getDocument(){
		return mDocument;
	}
	
	/**
	 * @return the original, custom string of data representing a svg visualization
	 */
	public String getSerializedDocument() {
		return mSerializedDocument;
	}

	/**
	 * Adds a new DocumentPreparedListener to this visualizationholder
	 * @param listener the listener to add
	 */
	public void setOnDocumentPreparedListener(DocumentPreparedListener listener){
		if (mDocumentPreparedListeners == null){
			mDocumentPreparedListeners = new ArrayList<DocumentPreparedListener>(1);
		}
		mDocumentPreparedListeners.add(listener);
	}

	/**
	 * Adds a new VisualizationEventListener to this visualizationholder
	 * @param listener the listener to add
	 */
	public void setOnVisualizationEventListener(VisualizationEventListener listener){
		if (mVisualizationEventListeners == null){
			mVisualizationEventListeners = new ArrayList<VisualizationEventListener>(1);
		}
		mVisualizationEventListeners.add(listener);
	}

	/**
	 * Notifies all attached OnDocumentPreparedListeners of the successful parsing of a document
	 * @param document the document that has been parsed by the UIManager
	 */
	public void notifyOnDocumentPreparedListeners(SVGDocument document){
		if (mDocumentPreparedListeners != null){
			for (DocumentPreparedListener listener : mDocumentPreparedListeners){
				if (listener != null)
					listener.onDocumentReady(document);
			}
		}
	}

	/**
	 * Notifies all attached OnVisualizationEventListeners of a new VisualizationEvent
	 * @param event the VisualizationEvent to send
	 */
	public void notifyOnVisualizationEventListeners(VisualizationEvent event){
		if (mVisualizationEventListeners != null){
			for (VisualizationEventListener listener : mVisualizationEventListeners){
				if (listener != null)
					listener.onVisualizationEvent(event);
			}
		}
	}
	
	/**
	 * Listens for mouse events related to the SVG object with the given id
	 * @param id the id of the SVG object to listen for events from
	 */
	public void listenTo(String id) {
		mSubscribedObjects.add(id);
	}
	
	/**
	 * @return an array representing the subscribed SVG ids added with "listenTo"
	 */
	public String[] getSubscribedObjects() {
		return mSubscribedObjects.toArray(new String[0]);
	}
}
